/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;


import Negocio.Cliente;
import Negocio.Empresa;
import Negocio.Personal;
import Negocio.Rol;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Diego
 */
public class Persistencia {
    private SessionFactory sessionFactory;
    private Session session;
    public Persistencia() {
    sessionFactory = new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();
    }
    public void insert(Object unObjeto) {
    Transaction tr = session.beginTransaction();
    session.save(unObjeto);
    tr.commit();
    }
    public void update(Object unObjeto){
        Transaction tr = session.beginTransaction();
            session.saveOrUpdate(unObjeto);
        tr.commit();
    }
    public void delete(Object unObjeto){
        Transaction tr = session.beginTransaction();
        session.delete(unObjeto);
        tr.commit();
    }
    
   
    public Empresa cargarCargarEmpresa(Integer unId) throws Exception{
        Empresa unaEmpresa = null;
        try{
            unaEmpresa = (Empresa) session.get(Empresa.class, unId);
        }
        catch(Exception ex){
            throw new Exception("Error al cargar la empresa: "+ex.getMessage());
        }
        return unaEmpresa;
    }
    
    public Personal cargarPersonal(Integer unId)throws Exception{
        Personal unPersonal = null;
        try{
            unPersonal = (Personal) session.get(Personal.class, unId);
        }
        catch(Exception ex){
            throw new Exception("Error al cargar el Personal: "+ex.getMessage());
        }
        return unPersonal;
    }
    
    public Cliente cargarCliente(Integer unId) throws Exception{
        Cliente unCliente = null;
        try{
            unCliente = (Cliente) session.get(Cliente.class, unId);
        }
        catch(Exception ex){
            throw new Exception("Error al cargar el Cliente");
        }
        return unCliente;
    }
    
    public Rol cargarRol(Integer unId) throws Exception{
        Rol unRol = null;
        try{
            unRol = (Rol) session.get(Rol.class, unId);
        }
        catch(Exception ex){
            throw new Exception("Error al cargar el Cliente");
        }
        return unRol;
    }

 
}
